package com.jh3502.vivo.function.account;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.dialog.BankChooseDialog;
import com.jh3502.vivo.utils.SpHelper;
import com.jh3502.vivo.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/11/26 15:30
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class AccountActivity extends BaseActivity<AccountPresenter.AccountView, AccountPresenter> implements AccountPresenter.AccountView {

    public static final int FROM_BANK_CHOOSE = 8;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.include_user_detail_name)
    TextView includeUserDetailName;
    @BindView(R.id.include_user_detail_com)
    TextView includeUserDetailCom;
    @BindView(R.id.account_name)
    EditText accountName;
    @BindView(R.id.account_num)
    EditText accountNum;
    @BindView(R.id.account_bank)
    TextView accountBank;
    private String id;

    @Override
    public AccountPresenter initPresenter() {
        return new AccountPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_account;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("银行账户");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        includeUserDetailName.setText(SpHelper.getInstance().getUserName());
        includeUserDetailCom.setText(SpHelper.getInstance().getDeptName());

        mPresenter.setAccountView(this);
        Map<String, Object> body = new HashMap<>(2);
        body.put("userId", SpHelper.getInstance().getUid());
        mPresenter.toFindaccount(body);


    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_done, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_done:
                Map<String, Object> body = new HashMap<>(2);
                body.put("id", id);
                body.put("accountName", accountName.getText().toString().trim());
                body.put("accountNum", accountNum.getText().toString().trim());
                body.put("openBank", accountBank.getText().toString().trim());
                mPresenter.toModifyaccount(body);
                break;
            default:
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case FROM_BANK_CHOOSE:
                    accountBank.setText(data.getStringExtra("bankName"));
                    break;
                default:
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }


    @Override
    public void findaccountSuccess(String s) {
        if (!"null".equals(s)) {
            Map<String, Object> data = Utils.getInstance().jso2map(s);
            accountName.setText((String) data.get("accountName"));
            accountNum.setText((String) data.get("accountNum"));
            accountBank.setText((String) data.get("openBank"));
            id = (String) data.get("id");
        }

    }

    @Override
    public void modifyaccountSuccess() {
        showShortToast("保存成功");
    }


    @OnClick(R.id.account_bank)
    public void onAccountBankClicked() {
        Intent intent = new Intent(getActContext(), BankChooseDialog.class);
        startActivityForResult(intent, FROM_BANK_CHOOSE);
    }

}
